package com.uinnova.product.eam.mvc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.binary.framework.util.ControllerUtils;
import com.binary.framework.web.RemoteResult;
import com.binary.jdbc.Page;
import com.uinnova.product.eam.model.cj.domain.TemplateType;
import com.uinnova.product.eam.model.cj.request.PlanDesignInstanceAddRequest;
import com.uinnova.product.eam.model.cj.vo.DiagramPlanVO;
import com.uinnova.product.eam.model.es.PlanProcessRelation;
import com.uinnova.product.eam.service.PlanService;
import com.uinnova.product.eam.vo.CheckPlanModuleReq;
import com.uinnova.product.eam.vo.CheckPlanModuleRes;
import com.uinnova.product.eam.vo.PlanPublishDirInfo;
import com.uinnova.product.eam.vo.PlanPublishDirReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController {

    @Autowired
    private PlanService planService;

    @PostMapping("/get/publish/dir")
    public RemoteResult list(@RequestBody PlanPublishDirReq req) {
        PlanPublishDirInfo dirInfo = planService.getPlanPublishDirInfo(req);
        return new RemoteResult(dirInfo);
    }

    @PostMapping("/save")
    public RemoteResult save(@RequestBody PlanDesignInstanceAddRequest request) {
        Long planId = planService.save(request);
        return new RemoteResult(planId);
    }

    @PostMapping("/getAllPlans")
    public RemoteResult getAllPlans(@RequestBody String body) {
        JSONObject jsonObject = JSON.parseObject(body);
        Page<DiagramPlanVO> result = planService.getAllPlans(jsonObject);
        return new RemoteResult(result);
    }

    /**
     * 查询方案模板类型及方案模板列表
     */
    @GetMapping("/getTemplateList")
    public void getTemplateList(HttpServletRequest request, HttpServletResponse response) {
        List<TemplateType> result = planService.getTemplateList();
        ControllerUtils.returnJson(request, response, result);
    }

    /**
     * 根据方案id，章节id，内容块id-判断相应的模块是否存在
     * @param req
     * @return
     */
    @PostMapping("/checkPlanModule")
    public RemoteResult checkPlanModule(@RequestBody CheckPlanModuleReq req) {
        CheckPlanModuleRes result = planService.checkPlanModule(req);
        return new RemoteResult(result);
    }

    /**
     * 已完结状态 根据流程id和架构设计中的方案id查询对应设计资产中对应版本的方案信息
     * @param processInstanceId
     * @param planId
     * @return
     */
    @GetMapping("/getPlanByProcessId")
    public RemoteResult getPlanByProcessId(@RequestParam String processInstanceId, @RequestParam Long planId) {
        Assert.notNull(processInstanceId, "processInstanceId不能为空");
        Assert.notNull(planId, "planId不能为空");
        PlanProcessRelation result = planService.getPlanByProcessId(processInstanceId, planId);
        return new RemoteResult(result);
    }
}
